/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.server.testing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class HierarchyBeanHolder<A extends Object>
{

	private final A bean;
	private final List<HierarchyBeanHolder<A>> children = new ArrayList<HierarchyBeanHolder<A>>();

	public HierarchyBeanHolder(A bean)
	{
		this.bean = bean;
	}

	public abstract String getId();

	public abstract String getParentId();

	public A getBean()
	{
		return bean;
	}

	public List<HierarchyBeanHolder<A>> getChildren()
	{
		return children;
	}

	public static <X extends Object> List<HierarchyBeanHolder<X>> createHierarchy(List<HierarchyBeanHolder<X>> beans)
	{
		final Map<String, HierarchyBeanHolder<X>> map = new HashMap<String, HierarchyBeanHolder<X>>();
		for (final HierarchyBeanHolder<X> bean : beans)
		{
			map.put(bean.getId(), bean);
		}
		final Set<String> modelsWithParent = new HashSet<String>();
		for (final HierarchyBeanHolder<X> bean : beans)
		{
			final HierarchyBeanHolder<X> parent = map.get(bean.getParentId());
			if (parent != null)
			{
				parent.getChildren().add(bean);
				modelsWithParent.add(bean.getId());
			}
		}
		final List<HierarchyBeanHolder<X>> rootElements = new ArrayList<HierarchyBeanHolder<X>>();
		for (final HierarchyBeanHolder<X> h : beans)
		{
			if (!modelsWithParent.contains(h.getId()))
			{
				rootElements.add(h);
			}
		}
		return rootElements;
	}

	private final String tab = "  ";

	public static <X> List<String> debug(final List<HierarchyBeanHolder<X>> beans)
	{
		return debugWithTab(beans, 0);
	}

	public static <X> List<String> debug(HierarchyBeanHolder<X> bean)
	{
		final int depth = 0;
		final List<String> debug = new ArrayList<String>();
		debug.add(bean.toStringWithTab(depth));
		debug.addAll(debugWithTab(bean.getChildren(), depth + 1));
		return debug;
	}

	public static <X> List<String> debugWithTab(List<HierarchyBeanHolder<X>> beans, int depth)
	{
		final List<String> debug = new ArrayList<String>();
		for (final HierarchyBeanHolder<X> bean : beans)
		{
			debug.add(bean.toStringWithTab(depth));
			debug.addAll(debugWithTab(bean.getChildren(), depth + 1));
		}
		return debug;
	}

	protected String toStringWithTab(int depth)
	{
		final StringBuffer sb = new StringBuffer(1000);
		for (int i = 0; i < depth; i++)
		{
			sb.append(tab);
		}
		sb.append(bean);
		return sb.toString();
	}

}
